const Actions = require('./actions');
const DB = require('app/db');
// [
//     {
//         subject: "合同签订 (2017.06.30）", date: "2017.01.10",content: [
//             { title: "XXXXXX成果 2017.6.30", date: "2017.01.10", badge_text: "已完成" },
//         ]
//     },
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        listData: []
    },
    onFetch: function (params, cb) {
        let t = this;
        DB.Approval.get_approval_detail({ process_id: params.process_id })
            .then((content) => {
                let nodelists = content.form_component_values.nodelists;
                let array = [];
                let content_arr = [];
                let obj = {};
                let sub_obj = {};
                for (let i = 0; i < nodelists.length; i++) {
                    let time = '';
                    for (let n = 0; n < nodelists[i].node_info.length; n++) {
                        time += n > 0 ? `-${nodelists[i].node_info[n].value}` : `${nodelists[i].node_info[n].value}`;
                    }
                    obj.subject = `${nodelists[i].node_name} (${time})`;
                    obj.node_color = nodelists[i].node_state==2?"green":(nodelists[i].node_state==1?"":"red");
                    let has = false;//是否含有已完成的成果
                    if (nodelists[i].node_achievements.length > 0) {
                        let node = nodelists[i].node_achievements;                       
                        for (let j = 0; j < node.length; j++) {
                            let time = node[j].time.replace(/\//g,".");
                            sub_obj = { title: node[j].achievements_name+" "+time, date: time, badge_text: (node[j].status == 1 ? "未完成" :(node[j].status == 0?"超时":"已完成") ) }
                            if(node[j].status == 2){
                                has = true;
                            }
                            content_arr.push(sub_obj);
                        }                      
                    }
                     //没有审批通过的成果物，且时间比设定的时间大为超时
                     if(i != 0){
                        let end_time = time.split("-")[1];
                        end_time += " 23:59:59";
                        let a = new Date().getTime();
                        let b = new Date(end_time).getTime();
                        
                        if(new Date().getTime() > new Date(end_time).getTime() && !has){
                            obj.node_color = "red";
                        }                   
                    }
                    obj.content = content_arr;
                    array.push(obj);
                    obj = {};
                    sub_obj = {};
                    content_arr = [];
                }
                this.data.listData = array;
                
                t.updateComponent();
            })
            .catch((error) => {
                cb && cb(t.data);
            });
    },

    updateComponent: function () {
        this.trigger(this.data);
    }
});
